//
//  TMGRealNameViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/9.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGRealNameViewController.h"
#import "STDidRealNameViewController.h"
@interface TMGRealNameViewController ()
@property(nonatomic, strong) UITextField                     *nameTextField;
@property(nonatomic, strong) UITextField                     *cardTextField;
@property(nonatomic, strong) NSArray<STTableEasyModel*>                     *dataSource;

@property(nonatomic, strong) STButton                     *cardButton;
@property(nonatomic, strong) STButton                     *cardBackButton;
@property(nonatomic, strong) STButton                     *desButton;

@property(nonatomic, strong) UIImage                     *originImag;
@end

@implementation TMGRealNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"实名认证";
    self.originImag = [UIImage imageNamed:@"sz_xiangce"];
    [self configSubView];
    [self configFooterView];

    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 20)];
    self.nameTextField.placeholder = @"请输入姓名";
    
    self.cardTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 20)];
    self.cardTextField.placeholder = @"请输入身份证号码";
    
    
    
    self.dataSource = @[
                        [[STTableEasyModel alloc] initWithTextString:@"姓名" accessoryView:self.nameTextField],
                        [[STTableEasyModel alloc] initWithTextString:@"身份证号" accessoryView:self.cardTextField]];
    [self.tableView reloadData];
    
}
- (void)configFooterView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 130)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [footer addSubview:whiteView];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, 20, 85, 20)
                                                     text:@"上传证件照片"
                                                textColor:TM_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [NSString lableAutoAdjustWitdhWithLabel:titleLable];
    [whiteView addSubview:titleLable];
    
    self.cardButton = [[STButton alloc] initWithFrame:CGRectMake(titleLable.right + 10, titleLable.top, 100, 100)
                                                title:@""
                                           titleColor:nil
                                            titleFont:0
                                         cornerRadius:0
                                      backgroundColor:TM_backgroundColor
                                      backgroundImage:nil
                                                image:_originImag];
    self.cardButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.cardButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedCarButton];
    }];
    [whiteView addSubview:self.cardButton];
    
    self.cardBackButton = [[STButton alloc] initWithFrame:CGRectMake(self.cardButton.right + 10, titleLable.top, 100, 100)
                                                    title:@""
                                               titleColor:nil
                                                titleFont:0
                                             cornerRadius:0
                                          backgroundColor:TM_backgroundColor
                                          backgroundImage:nil
                                                    image:_originImag];
    self.cardBackButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.cardBackButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedCarBackButton];
    }];
    [whiteView addSubview:self.cardBackButton];
    
    
    STButton * desButton = [[STButton alloc] initWithFrame:CGRectMake(15, 0, 44, 44)
                                                     title:@""
                                                titleColor:TM_secendTextColor
                                                 titleFont:13
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"icon_选中"]];
    [desButton setImage:[UIImage imageNamed:@"icon_未选中"] forState:UIControlStateSelected];
    self.desButton = desButton;
    [desButton setClicAction:^(UIButton *sender) {
        sender.selected = !sender.selected;
    }];
    desButton.selected = YES;
    desButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:desButton];
    desButton.top = whiteView.bottom + 10;
    
    STButton *titleButton = [[STButton alloc] initWithFrame:CGRectMake(desButton.right, 0, UIScreenWidth, 44)
                                                      title:@"我已经认真阅读并同意黄金口袋身份认证协议"
                                                 titleColor:TM_secendTextColor
                                                  titleFont:13
                                               cornerRadius:0
                                            backgroundColor:nil
                                            backgroundImage:nil
                                                      image:nil];
    [titleButton setAttributedTitle:[titleButton.currentTitle st_convertAttributeStringWithKeyWord:@"黄金口袋身份认证协议" attributes:@{NSForegroundColorAttributeName:TM_orangeColor}] forState:UIControlStateNormal];
    [titleButton setClicAction:^(UIButton *sender) {
        
        NSString * url = [NSString stringWithFormat:@"http://jshjkd.com/gold/public/home/web/contract?cate=%@",
                          @"9"];
        STWebViewController * webvc = [[STWebViewController alloc] initWithUrl:url];
        webvc.progressBackgroundColor = [UIColor whiteColor];
        webvc.progressTintColor = TM_orangeColor;
        webvc.forceTitle = @"身份认证协议";
        [weakSelf.navigationController pushViewController:webvc animated:YES];
    }];
    titleButton.centerY = desButton.centerY;
    [footer addSubview:titleButton];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60 , 44)
                                                        title:@"确认修改"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:TM_orangeColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedConfimButton];
    }];
    confimButton.top = titleButton.bottom  +40;
    
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    footer.height = confimButton.bottom + 20;
    self.tableView.tableFooterView = footer;
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    STTableEasyModel * model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.textString;
    cell.detailTextLabel.text = model.detailString;
    if (model.accessoryView) {
        cell.accessoryView = model.accessoryView;
    }else{
        cell.accessoryType = model.accessoryType;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)onSelctedCarButton{
    [self showDefultImagePicker:^(UIImage *image) {
        [self.cardButton setImage:image forState:UIControlStateNormal];
    }];
}
- (void)onSelctedCarBackButton{
    [self showDefultImagePicker:^(UIImage *image) {
        [self.cardBackButton setImage:image forState:UIControlStateNormal];
    }];
}
- (void)onSelctedConfimButton{
    if (!self.nameTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入姓名"];
        return;
    }
    if (!self.cardTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入身份证号码"];
        return;
    }
    if (self.cardButton.currentImage == self.originImag) {
        [SVProgressHUD showErrorWithStatus:@"请上传身份证正面照"];
        return;
    }
    if (self.cardBackButton.currentImage == self.originImag) {
        [SVProgressHUD showErrorWithStatus:@"请上传身份证背面照"];
        return;
    }
    if (self.desButton.selected) {
        [SVProgressHUD showErrorWithStatus:@"请阅读并且同意黄金口袋身份认证协议"];
        return;
    }
    [self sendLoadRealNameRequest];
}

- (void)sendLoadRealNameRequest{
    
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/verify"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (self.nameTextField.text.length) {
        [dic setObject:self.nameTextField.text forKey:@"name"];
    }
    if (self.cardTextField.text.length) {
        [dic setObject:self.cardTextField.text forKey:@"idcard"];
    }
    [[STNetWrokManger defaultClient] imagesRequestWithPath:url
                                                parameters:dic images:@[self.cardButton.currentImage,self.cardBackButton.currentImage]
                                                imageNames:@[@"front",@"back"]
                                                  progress:^(NSProgress *progress) {
                                                      
                                                  } success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                      
                                                      [SVProgressHUD dismiss];
                                                      [SVProgressHUD showSuccessWithStatus:@"已成功提交申请"];
                                                  } failure:^(NSString *stateCode, STError *error) {
                                                      [SVProgressHUD dismiss];
                                                      [SVProgressHUD showErrorWithStatus:error.desc];
                                                  }];
    
}
@end

